<?php

namespace app\index\service;

use app\common\model\ConsultingQuestion;
use app\common\model\LawyerImpression;
use app\common\model\Member;
use app\common\service\ARYPushQueueService;
use app\index\dao\ConsultingQuestionDao;
use app\index\dao\MemberDao;
use app\index\model\UM as UM;

/**
 * Class QuestionService
 * @package app\index\service
 */
class QuestionService
{
    // dao
    private ConsultingQuestionDao $consultingQuestionDao;
    private MemberDao $memberDao;
    private LawyerImpressionService $impressionService;

    public function __construct(ConsultingQuestionDao $consultingQuestionDao, MemberDao $memberDao, LawyerImpressionService $impressionService)
    {
        $this->consultingQuestionDao = $consultingQuestionDao;
        $this->memberDao = $memberDao;
        $this->impressionService = $impressionService;
    }

    /**
     * 查询
     * @param UM|null $user
     * @param int $id
     * @return ConsultingQuestion
     */
    public function read(?UM $user, int $id): ?ConsultingQuestion
    {
        $userId = $user ? $user->id : 0;

        //这里如果要修改查询，建议把查询放到dao层去
        $record = $this->consultingQuestionDao->findDetail($id);

        if (!$record) {
            return null;
        }

        $related = $this->consultingQuestionDao->selectRelated($record->categoryId, [$record->id], 2);
        $record->setRelation('related', $related);
        // 记录曝光行为
        $related->each(function (ConsultingQuestion $question) use ($userId) {
            $this->impressionService->insert(LawyerImpression::ZIXUN_ANLI, $question->lawyerId, $userId);
        });

        $lawyers = $this->memberDao->selectLawyersByCategory($record->categoryId, [$record->lawyerId], 3);
        $record->setRelation('lawyers', $lawyers);
        // 记录曝光行为
        $lawyers->each(function (Member $lawyer) use ($userId) {
            $this->impressionService->insert(LawyerImpression::ZIXUN_LVSHI, $lawyer->id, $userId);
        });

        // 触发咨询查看事件
        event('QuestionRead', ['questionId' => $record->id]);

        return $record;
    }

    /**
     * 保存问题
     * @param $memberId
     * @param $ask
     * @param $categoryId
     * @param string $sourceType
     * @param string $fromType
     * @param string $channel
     * @return ConsultingQuestion
     */
    public function saveQuestion($memberId, $ask, $categoryId, string $sourceType = '', string $fromType = '', bool $push = true,
                                 string $channel = '', string $pf = '', string $chain = '', $promotionGroupId = '0'): ConsultingQuestion
    {

        $params = [
            'memberId' => $memberId,
            'ask' => $ask,
            'categoryId' => $categoryId,
            'sourceType' => $sourceType,
            'fromType' => $fromType,
            'channel' => $channel,
            'pf' => $pf,
            'chain' => $chain,
            'promotionGroupId' => $promotionGroupId,
        ];

        $record = $this->consultingQuestionDao->insertActive($params);

        // 延迟推送到案如云
        if ($push) {
            $this->getARYPushQueueService()->pushQuestion($record->id);
        }

        return $record;
    }

    private function getARYPushQueueService(): ARYPushQueueService
    {
        return app(ARYPushQueueService::class);
    }
}
